#include "stdafx.h"
#include "Game.h"
#include "StateManager.h"
#include "IntroState.h"
#include "ObjectManager.h"
#include "BaseObject.h"
#include "TextureManager.h"
#include "StateManager.h"
#include "DirectInput.h"
#include "Camera.h"

#include "BMPText.h"

//Temporary States
#include "GameplayState.h"
#include "MainMenuState.h"
#include "FModManager.h"
#include "Player.h"

//////////////////////////////////////////////////////////////////////////

Game::Game() :
	m_Application(NULL),
	m_Window(NULL),
	m_Timer(),
	m_pPlayer(NULL)
{
}

Game::~Game()
{
}

void Game::InitGame(HWND hWnd, HINSTANCE hInstance)
{
	m_bRunning = true;
	m_Application = hInstance;

	TextureManager::GetInstance()->InitTextureManager(g_pD3DDevice, g_pD3DSprite);
 	//Input::GetInstance()->Init();

// 	FMod::GetInstance()->Init();
	BMPText::GetInstance()->InitFonts(NULL);

	//Should be intro state
	StateManager::GetInstance()->Push(MainMenuState::GetInstance());
	DirectInput::GetInstance()->InitDirectInput(hWnd, hInstance, DI_KEYBOARD | DI_MOUSE);
	m_Timer.Reset();

	FModManager::GetInstance()->InitFModManager(hWnd);
}

void Game::EndGame()
{
	/*
	g_StateManagerInstance->Pop();
	g_ObjectManagerInstance->RemoveAllObjects();
	g_FModInstance->Shutdown();
	g_InputInstance->Shutdown();
	g_AngelRendererInstance->Shutdown();
	*/

	


	DirectInput::GetInstance()->ShutdownDirectInput();
	ObjectManager::GetInstance()->RemoveAllObjects();
	TextureManager::GetInstance()->ShutdownTextureManager();
	FModManager::GetInstance()->ShutdownFModManager();
}

void Game::UpdateGame()
{
	static float dt = 0.0f;
	dt += m_Timer.GetDeltaTime();

	if (dt > 0.5f) // HACK for when device reset stalls in the function call
	{
		dt = 0.0f;
		m_Timer.Update();
		return;
	}

	if (dt == 0.0f)
		dt = FLT_MIN;

	// Update everything
	
// 	Input::GetInstance()->Update();
// 	StateManager::GetInstance()->Update(dt);
	
	D3DXMATRIX view;

	DirectInput::GetInstance()->ReadDevices();
	ObjectManager::GetInstance()->UpdateObjects(dt);
	StateManager::GetInstance()->Update(dt);

 	CCamera* pCamera = CCamera::GetInstance(); 
	pCamera->ProcessCamera(dt);
// 	pCamera->CalculateViewMatrix(&view);
// 	g_pD3DDevice->SetTransform(D3DTS_VIEW, &view);
// 	zFMod::GetInstance()->Update(dt);

// 	//render
// 	m_pD3D->Clear(0,0,0);
// 	m_pD3D->DeviceBegin();
// 	m_pD3D->SpriteBegin();
// 
// 	//do rendering here
// 	StateManager::GetInstance()->Render();
// 
// 	m_pD3D->SpriteEnd();
// 	//texting out should be done here
// 
// 	m_pD3D->DeviceEnd();
// 	m_pD3D->Present();


		
	dt = 0.0f;

	m_Timer.Update();
}

void Game::RenderGame()
{
	//State render
	StateManager::GetInstance()->Render();
}